<template>
  <div class="card col p10">
    <label class="title">小部件控制</label>
    <div class="row_center mt">
      <el-switch @click="onClickPastaFan" v-model="ctx.pasta_fan" active-text="煮面室风扇"/>
      <el-switch @click="onClickLight" class="ml" v-model="ctx.light" active-text="外部照明"/>
    </div>
    <div class="row_center mt">
      <el-switch @click="onClickFridgeFan" v-model="ctx.fridge_fan" active-text="冰箱顶部风扇"/>
      <el-switch @click="onClickPastaLed" class="ml" v-model="ctx.pasta_led" active-text="煮面室LED"/>
    </div>
    <div class="row_center mt">
      <el-switch @click="onClickFridgeLed" v-model="ctx.fridge_led" active-text="冰箱LED"/>
      <el-switch @click="onClickTrayHeat" class="ml" v-model="ctx.tray_heat" active-text="滑块加热器"/>
    </div>
    <div class="row_center mt">
      <el-switch @click="onClickTrackHeat" v-model="ctx.track_heat" active-text="导轨加热器"/>
      <el-switch class="ml" @click="onClickCho" v-model="ctx.cho" active-text="取筷门"/>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { tool_ctrl } from '../../service/main_cmd'
import { dialog } from '../../service/ui';


interface Ctx {
  pasta_fan: boolean;
  light: boolean;
  fridge_fan: boolean;
  pasta_led: boolean;
  fridge_led: boolean;
  tray_heat: boolean;
  track_heat: boolean;
  cho: boolean;
}

const ctx = reactive<Ctx>({
  pasta_fan: false,
  light: false,
  fridge_fan: false,
  pasta_led: false,
  fridge_led: false,
  tray_heat: false,
  track_heat: false,
  cho: false,
});

async function onClickPastaFan() {
  let val = 1;
  if (ctx.pasta_fan) {
    val = 0;
  }
  try {
    await tool_ctrl(1, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickLight() {
  let val = 1;
  if (ctx.light) {
    val = 0;
  }
  try {
    await tool_ctrl(2, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickFridgeFan() {
  let val = 1;
  if (ctx.fridge_fan) {
    val = 0;
  }
  try {
    await tool_ctrl(3, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickPastaLed() {
  let val = 1;
  if (ctx.pasta_led) {
    val = 0;
  }
  try {
    await tool_ctrl(4, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickFridgeLed() {
  let val = 1;
  if (ctx.fridge_led) {
    val = 0;
  }
  try {
    await tool_ctrl(5, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickTrayHeat() {
  let val = 1;
  if (ctx.tray_heat) {
    val = 0;
  }
  try {
    await tool_ctrl(6, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickTrackHeat() {
  let val = 1;
  if (ctx.track_heat) {
    val = 0;
  }
  try {
    await tool_ctrl(7, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickCho() {
  let val = 1;
  if (ctx.cho) {
    val = 0;
  }
  try {
    await tool_ctrl(8, val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}


</script>
